package com.hopster;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CalendarPage extends Activity {
	
	public Month m;
	GridView monthView;
    Drawable draw;
	TextView month;
	private Button prev, next;
	
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar);

        month = (TextView) findViewById(R.id.monthtextview);
        m = new Month();
        monthView = (GridView) findViewById(R.id.gridview);
        monthView.setAdapter(new DayAdapter(this));
        month.setText(m.getMonthId() + " - " + m.getYear());
        
        prev 	= (Button) findViewById(R.id.prev);
		next 	= (Button) findViewById(R.id.next);
		//draw	= (Drawable).getResources.getDrawable(R.drawable.button);
        
        //Initialize days as clickable
        monthView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            	if (position > (6+m.preNonDays) && position < (49-m.postNonDays)){
            		Intent i = new Intent();
            		Bundle date = m.passDate(position - (6+m.preNonDays));
            		i.setClassName("com.hopster", "com.hopster.EventPage");
            		i.putExtras(date);
            		startActivity(i);
            	}
        	}
        });
        
		//previous month button
        prev.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {	
            	m.prev();
            	month.setText(m.getMonthId() + " - " + m.getYear());
            	monthView.setAdapter(new DayAdapter(getApplicationContext()));
            }
        });
		
        //next month button
		next.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {	
				m.next();
				month.setText(m.getMonthId() + " - " + m.getYear());
				monthView.setAdapter(new DayAdapter(getApplicationContext()));
            }
        });
    }
	
	@Override
    protected void onResume()
    {
        super.onResume();
        m.setMonth();
        monthView.setAdapter(new DayAdapter(getApplicationContext()));
    }
        
	
	public class DayAdapter extends BaseAdapter
	{//class to format each day
		
		private Context mContext;
		private String dayv;
		

		//Constructor
	    public DayAdapter(Context c) {
	        mContext = c;
	        
	    }

	  //default adapter methods
	    public int getCount() {
	    	if(m.extra)
	    	{
	    		return 49;
	    	}
	    	else
	    	{
	    		return 42;
	    	}
	        
	    }

	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }
	    
	    

	    // create a new View for each item referenced by the Adapter
	    public View getView(int position, View convertView, ViewGroup parent) {
	        TextView day;
	        if (convertView == null) {  // if it's not recycled, initialize some attributes
	            day = new TextView(mContext);
	            day.setLayoutParams(new GridView.LayoutParams(65, 70));
	            day.setTextSize(10);
	            day.setBackgroundColor(-1);
	            day.setTextColor(Color.BLACK);
	            day.setBackgroundDrawable(getResources().getDrawable(R.drawable.button));
	            
	        } 
	        else {//recycle formatting attributes for each day
	            day = (TextView) convertView;
	        }
	        
	        dayv = m.dayFormat(position);

	        day.setText(dayv);
	        return day;
	    }
	    

	}
	
}
